package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunBodyGroupMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunBodyGroup;
import com.csun.cmny.provider.model.domain.DmcCsunBodyGroupBind;
import com.csun.cmny.provider.model.dto.bodysensor.BodyGroupDto;
import com.csun.cmny.provider.model.dto.bodysensor.BodyGroupQueryDto;
import com.csun.cmny.provider.model.vo.csunbody.BodyGroupVo;
import com.csun.cmny.provider.service.DmcCsunBodyGroupBindService;
import com.csun.cmny.provider.service.DmcCsunBodyGroupService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-22 15:52
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcCsunBodyGroupServiceImpl extends BaseService<DmcCsunBodyGroup>
        implements DmcCsunBodyGroupService {

    @Resource
    private DmcCsunBodyGroupMapper dmcCsunBodyGroupMapper;
    @Resource
    private DmcCsunBodyGroupBindService dmcCsunBodyGroupBindService;

    /**
     * save dmc csun body group
     *
     * @param bodyGroupDto the body group dto
     * @param loginAuthDto the login auth dto
     * @return the int.
     */
    @Override
    public Integer save(BodyGroupDto bodyGroupDto, LoginAuthDto loginAuthDto) {

        DmcCsunBodyGroup dmcCsunBodyGroup;

        if (PublicUtil.isNotEmpty(bodyGroupDto.getId())) {
            dmcCsunBodyGroup = dmcCsunBodyGroupMapper.selectByPrimaryKey(bodyGroupDto.getId());
            if (PublicUtil.isEmpty(dmcCsunBodyGroup)) {
                throw new DmcBizException(ErrorCodeEnum.DMC100619001);
            }

            if (PublicUtil.isNotEmpty(bodyGroupDto.getRemark())) {
                dmcCsunBodyGroup.setRemark(bodyGroupDto.getRemark());
            }

            if (PublicUtil.isNotEmpty(bodyGroupDto.getTimeOut())) {
                dmcCsunBodyGroup.setTimeOut(bodyGroupDto.getTimeOut());
            }

            if (PublicUtil.isNotEmpty(bodyGroupDto.getLocation())) {
                dmcCsunBodyGroup.setLocation(bodyGroupDto.getLocation());
            }

            if (PublicUtil.isNotEmpty(bodyGroupDto.getOldmanName())) {
                dmcCsunBodyGroup.setOldmanName(bodyGroupDto.getOldmanName());
            }

            if (PublicUtil.isNotEmpty(bodyGroupDto.getOldmanId())) {
                dmcCsunBodyGroup.setOldmanId(bodyGroupDto.getOldmanId());
            }

            dmcCsunBodyGroup.setUpdateInfo(loginAuthDto);

            return dmcCsunBodyGroupMapper.updateByPrimaryKeySelective(dmcCsunBodyGroup);
        } else {

            dmcCsunBodyGroup = new DmcCsunBodyGroup();

            dmcCsunBodyGroup.setRemark(bodyGroupDto.getRemark());
            dmcCsunBodyGroup.setGroupId(loginAuthDto.getGroupId());
            dmcCsunBodyGroup.setUpdateInfo(loginAuthDto);
            dmcCsunBodyGroup.setTimeOut(bodyGroupDto.getTimeOut());
            dmcCsunBodyGroup.setLocation(bodyGroupDto.getLocation());
            dmcCsunBodyGroup.setOldmanName(bodyGroupDto.getOldmanName());
            dmcCsunBodyGroup.setOldmanId(bodyGroupDto.getOldmanId());

            return dmcCsunBodyGroupMapper.insertSelective(dmcCsunBodyGroup);
        }
    }

    /**
     * query body group list with page
     *
     * @param bodyGroupQueryDto the body group dto
     *
     * @return the list.
     */
    @Override
    public List<BodyGroupVo> queryListWithPage(BodyGroupQueryDto bodyGroupQueryDto) {

        PageHelper.startPage(bodyGroupQueryDto.getPageNum(), bodyGroupQueryDto.getPageSize());

        return dmcCsunBodyGroupMapper.queryListWithPage(bodyGroupQueryDto);
    }

    /**
     * delete by id
     *
     * @param id the id
     *
     * @return the int.
     */
    @Override
    public Integer deleteById(Long id) {

        // 判断是否存在绑定过
        DmcCsunBodyGroupBind dmcCsunBodyGroupBind = new DmcCsunBodyGroupBind();
        dmcCsunBodyGroupBind.setBodyGroupId(id);
        int count = dmcCsunBodyGroupBindService.selectCount(dmcCsunBodyGroupBind);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100619002);
        }

        return dmcCsunBodyGroupMapper.deleteByPrimaryKey(id);
    }
}
